package com.bp;

import java.sql.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Andhini
 */
public class Pemesan extends Paket {

    public String id_pemesan;
    public String nama_pemesan;
    public String alamat_pemesan;
    public String no_telp_pemesan;
    public String email_pemesan;
    public String keberangkatan;
    public int jumalah_wisatawan;
    private String idpaket;
    public String status;

    /**
     * @return the id_pemesan
     */
    public String getId_pemesan() {
        return id_pemesan;
    }

    /**
     * @param id_pemesan the id_pemesan to set
     */
    public void setId_pemesan(String id_pemesan) {
        this.id_pemesan = id_pemesan;
    }

    /**
     * @return the id_biro
     */
    public String getnama_paket() {
        return nama_paket;
    }

    /**
     * @param id_biro the id_biro to set
     */
    public void setNamaPaket(String nama_paket) {
        this.nama_paket = nama_paket;
    }

    /**
     * @return the id_paket
     */
    public String getId_paket() {
        return id_paket;
    }

    /**
     * @param id_paket the id_paket to set
     */
    public void setId_paket(String id_paket) {
        this.id_paket = id_paket;
    }

    /**
     * @return the nama_pemesan
     */
    public String getNama_pemesan() {
        return nama_pemesan;
    }

    /**
     * @param nama_pemesan the nama_pemesan to set
     */
    public void setNama_pemesan(String nama_pemesan) {
        this.nama_pemesan = nama_pemesan;
    }

    /**
     * @return the alamat_pemesan
     */
    public String getAlamat_pemesan() {
        return alamat_pemesan;
    }

    /**
     * @param alamat_pemesan the alamat_pemesan to set
     */
    public void setAlamat_pemesan(String alamat_pemesan) {
        this.alamat_pemesan = alamat_pemesan;
    }

    /**
     * @return the email_pemesan
     */
    public String getEmail_pemesan() {
        return email_pemesan;
    }

    /**
     * @param email_pemesan the email_pemesan to set
     */
    public void setEmail_pemesan(String email_pemesan) {
        this.email_pemesan = email_pemesan;
    }

    /**
     * @return the keberangkatan
     */
    public String getKeberangkatan() {
        return keberangkatan;
    }

    /**
     * @param keberangkatan the keberangkatan to set
     */
    public void setKeberangkatan(String keberangkatan) {
        this.keberangkatan = keberangkatan;
    }

    /**
     * @return the jumalah_wisatawan
     */
    public int getJumalah_wisatawan() {
        return jumalah_wisatawan;
    }

    /**
     * @param jumalah_wisatawan the jumalah_wisatawan to set
     */
    public void setJumalah_wisatawan(int jumalah_wisatawan) {
        this.jumalah_wisatawan = jumalah_wisatawan;
    }

    /**
     * @return the no_telp_pemesan
     */
    public String getNo_telp_pemesan() {
        return no_telp_pemesan;
    }

    /**
     * @param no_telp_pemesan the no_telp_pemesan to set
     */
    public void setNo_telp_pemesan(String no_telp_pemesan) {
        this.no_telp_pemesan = no_telp_pemesan;
    }
    
    public void insertPesanan(Pemesan p) {
        try {
            Connection connection = new DbBean_1().getKoneksi();
            Statement s = connection.createStatement();
           // System.out.println("haduhijd");
            String sql = "insert into pemesan values('" + p.getId_pemesan() + "','"
                    + p.getNama_pemesan() + "','" + p.getAlamat_pemesan() + "','"
                    + p.getEmail_pemesan() + "','" + p.getNo_telp_pemesan() + "','"
                    + p.getKeberangkatan() + "'," + p.getJumalah_wisatawan() + ",'"
                    + p.getidpaket() + "')";
            ResultSet rs = s.executeQuery(sql);
            JOptionPane.showMessageDialog(null, "data pesanan berhasil dipesan");
            rs.close();
            s.close();
            connection.close();
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "data paket gagal dipesan /n" + ex.getMessage());
        }
    }
    
    public final String getKode_Pesanan() {
            try {
            String sql = "select max(ID_PEMESAN) from PEMESAN";
            Connection con = new DbBean_1().getKoneksi();
            java.sql.Statement sta = con.createStatement();
            ResultSet rs = sta.executeQuery(sql);

            rs.next();
            String result = rs.getString(1);
            if (result != null) {
                int no = Integer.parseInt(result);
                no++;
                sql = "" + no;
                while (sql.length() <= 3) {
                    sql = "0" + sql;
                }
            } else {
                sql = "0001";
            }
            return sql;
        } catch (SQLException ex) {
            Logger.getLogger(Pemesan.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }
//        try {
////            String sql = "select ID_PAKET from IDPAKET desc";
//            String sql = "select Id_Pemesan from ID_PEMESAN";
//            Connection con = new DbBean_1().getKoneksi();
//            java.sql.Statement sta = con.createStatement();
//            ResultSet rs = sta.executeQuery(sql);
//            
//            rs.next();
//            
//            
//            String result = rs.getString(1);
//            int no = Integer.parseInt(result);
//            no++;
//            sql = "" + no;
//            while (sql.length() <= 3) {
//                sql = "0" + sql;
//            }
//            
//            sql = "update Id_Pemesan set ID_PEMESAN = '" + sql + "'";
//            sta = con.createStatement();
//            sta.executeUpdate(sql);
//            con.close();
//            
//            return result;
//            
//            
//        } catch (SQLException ex) {
//            return "";
//        }
//    }
//    
        public ArrayList lihatDataPemesanan() {
        ArrayList pemesans = new ArrayList();
        try {
            Connection connection = new DbBean_1().getKoneksi();
            Statement s = connection.createStatement();
            String sql = "SELECT * from Pemesan order by ID_PEMESAN";
            ResultSet rs = s.executeQuery(sql);
            while (rs.next()) {
                Pemesan pkt = new Pemesan();
                pkt.id_pemesan = rs.getString(1);
                pkt.nama_pemesan = rs.getString(2);
                pkt.alamat_pemesan = rs.getString(3);
                pkt.email_pemesan= rs.getString(4);
                pkt.no_telp_pemesan= rs.getString(5);
                pkt.keberangkatan= rs.getString(6);
                pkt.jumalah_wisatawan =Integer.parseInt( rs.getString(7));
                pkt.id_paket = rs.getString(8);
                pkt.status = rs.getString(9);
                pemesans.add(pkt);
            }
            rs.close();
            s.close();
            connection.close();
        } catch (SQLException e) {
        }
        return pemesans;
    }

    /**
     * @return the idPemesanan
     */
    public String getidpaket() {
        return idpaket;
    }

    /**
     * @param idPemesanan the idPemesanan to set
     */
    public void setidpaket(String idpaket) {
        this.idpaket = idpaket;
    }
    
//    public double Totalharga(double total) {        
//        total = jumalah_wisatawan * Double.parseDouble(harga_paket);
//        return total;
//    
//    
//    }
//    public String getHarga(String id_paket) {
//        String result = "";
//       PreparedStatement s =null;
//       ResultSet rs =null;
//        Connection con = new DbBean().getKoneksi();
//        String query = "select paket.harga_paket from paket,pemesan where paket.id_paket = pemesan.id_paket and pemesan.id_paket ='0001'";
//       s = con.prepareStatement(query);
//       s.setString(1, id_paket);
//       rs = s.executeQuery();
//       if (rs.next()){
//           result = rs.getString(1);
//       }
//        return result;
//    }

    /**
     * @return the status
     */
    public String getStatus() {
        return status;
    }

    /**
     * @param status the status to set
     */
    public void setStatus(String status) {
        this.status = status;
    }
    public Boolean updateStatus(String id_pemesan, String status) {
        int result = 0;
        try {
            Connection con = new DbBean_1().getKoneksi();
            PreparedStatement pstmt = (PreparedStatement) con.prepareStatement("UPDATE PEMESAN SET STATUS='"+status+"' WHERE ID_PEMESAN='" + id_pemesan + "'");
            result = pstmt.executeUpdate();
        } catch (SQLException ex) {
            System.out.println("message: " + ex.getMessage());
        }

        if (result > 0) {
            return true;
        } else {
            return false;
        }
    }
}
